package com.huangliusong.desigin.pattern.structual.proxy.staticproxy;

import com.huangliusong.desigin.pattern.structual.proxy.IOrderService;
import com.huangliusong.desigin.pattern.structual.proxy.Order;
import com.huangliusong.desigin.pattern.structual.proxy.OrderServiceImpl;
import com.huangliusong.desigin.pattern.structual.proxy.db.DataSourceContextHolder;

/**
 * @author huangliusong
 * @since 2018/10/13
 * {@link }
 */
public class OrderServiceStaticProxy {
    private IOrderService iOrderService;

    public int saveOrder(Order order){
        beforeMethod(order);
        iOrderService=new OrderServiceImpl();
        int result=iOrderService.saveOrder(order);
        afterMethod();
        return result;
    }

    private void beforeMethod(Order order){
        int userId=order.getUserId();
        int dbRouter=userId%2;
        System.out.println("静态代理分配到[DB"+dbRouter+"]处理数据");
        DataSourceContextHolder.setDBType(String.valueOf(dbRouter));
        System.out.println("静态代理before code");
    }

    private void afterMethod(){
        System.out.println("静态代理after code");
    }

}
